package com.meida.module.bus.provider.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusRecDeliveryBkView;
import com.meida.module.bus.client.entity.BusReceiptDeliveryRecordBack;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.mapper.BusRecDeliveryBkViewMapper;
import com.meida.module.bus.provider.service.BusRecDeliBkViewService;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.module.bus.provider.service.BusRecDeliBkReqRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * VIEW接口实现类
 *
 * @author flyme
 * @date 2020-12-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusRecDeliBkViewServiceImpl extends BaseServiceImpl<BusRecDeliveryBkViewMapper, BusRecDeliveryBkView> implements BusRecDeliBkViewService {
    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Autowired
    private BusRecDeliBkReqRecordService bkReckServices;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusRecDeliveryBkView brdbv, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusRecDeliveryBkView> cq, BusRecDeliveryBkView brdbv, EntityMap requestMap) {

        cq.notIn("syncStatus", ApiConstants.REC_DELIVERY_CANCEL);
        cq.like(BusRecDeliveryBkView.class, "recDecId");
        cq.like(BusRecDeliveryBkView.class, "syncStatus");
        cq.like(BusRecDeliveryBkView.class, "projectId");
        cq.like(BusRecDeliveryBkView.class, "projectCode");
        cq.like(BusRecDeliveryBkView.class, "projectName");
        cq.like(BusRecDeliveryBkView.class, "majorCode");
        cq.like(BusRecDeliveryBkView.class, "majorName");
        cq.like(BusRecDeliveryBkView.class, "packageNo");
        cq.like(BusRecDeliveryBkView.class, "packageName");
        cq.like(BusRecDeliveryBkView.class, "userId");
        cq.like(BusRecDeliveryBkView.class, "userName");
        cq.like(BusRecDeliveryBkView.class, "appTime");
        cq.like(BusRecDeliveryBkView.class, "purchaseNum");
        cq.like(BusRecDeliveryBkView.class, "onLineNum");
        cq.like(BusRecDeliveryBkView.class, "receiveNum");
        cq.like(BusRecDeliveryBkView.class, "receivedDate");
        cq.like(BusRecDeliveryBkView.class, "vendorId");
        cq.like(BusRecDeliveryBkView.class, "zid");
        cq.like(BusRecDeliveryBkView.class, "zitem");
        cq.like(BusRecDeliveryBkView.class, "action");
        cq.like(BusRecDeliveryBkView.class, "budat");
        cq.like(BusRecDeliveryBkView.class, "bldat");
        cq.like(BusRecDeliveryBkView.class, "bktxt");
        cq.like(BusRecDeliveryBkView.class, "supplierCode");
        cq.like(BusRecDeliveryBkView.class, "supplierName");
        cq.like(BusRecDeliveryBkView.class, "recNumThis");
        cq.like(BusRecDeliveryBkView.class, "reserveNo");
        cq.like(BusRecDeliveryBkView.class, "reserveRow");
        cq.like(BusRecDeliveryBkView.class, "lgort");
        cq.like(BusRecDeliveryBkView.class, "ext01");
        cq.like(BusRecDeliveryBkView.class, "ext02");
        cq.like(BusRecDeliveryBkView.class, "ext03");
        cq.like(BusRecDeliveryBkView.class, "ext04");
        cq.like(BusRecDeliveryBkView.class, "ext05");
        cq.like(BusRecDeliveryBkView.class, "msgtyFromSap");
        cq.like(BusRecDeliveryBkView.class, "msgtxFromSap");
        cq.like(BusRecDeliveryBkView.class, "mblnrFromSap");
        cq.like(BusRecDeliveryBkView.class, "mblppoFromSap");
        cq.like(BusRecDeliveryBkView.class, "mjahrFromSap");
        cq.like(BusRecDeliveryBkView.class, "createTime");
        cq.like(BusRecDeliveryBkView.class, "updateTime");
        cq.like(BusRecDeliveryBkView.class, "purchaseDetailsId");
        cq.like(BusRecDeliveryBkView.class, "materialWbsCode");
        cq.like(BusRecDeliveryBkView.class, "materialCode");
        cq.like(BusRecDeliveryBkView.class, "recNumOut");
        cq.like(BusRecDeliveryBkView.class, "charOffCountCan");
        List<String> majorCodes = pmsApiUtil.getAuthMajorCodes(OpenHelper.getUserId());
        if(null != majorCodes) {
            cq.in("majorCode", majorCodes);
        }
        List<String> projectCodeList = pmsApiUtil.getAuthProjectIds(OpenHelper.getUserId());
        if(null != projectCodeList) {
            cq.in("projectCode", projectCodeList);
        }
        cq.orderByDesc("brdbv.createTime");
        return ResultBody.ok();
    }

    @Override
    public void recDeliveryMkBk(List<BusRecDeliveryBkView> busRecDeliveryBkViews) {
        List<Long> inIns = Lists.newArrayList();
        Map<Long, BigDecimal> thisCountMap = Maps.newHashMap();
        busRecDeliveryBkViews.stream().forEach(inObj -> {
            inIns.add(inObj.getRecDecId());
            thisCountMap.put(inObj.getRecDecId(), inObj.getCharOffCount());
        });

        Collection<BusRecDeliveryBkView> inLists = listByIds(inIns);
        inLists.stream().forEach(inObj -> {
            inObj.setCharOffCount(thisCountMap.get(inObj.getRecDecId()));
        });
        List<BusReceiptDeliveryRecordBack> upds = Lists.newArrayList();
        inLists.stream().forEach(inObj -> {
            BusReceiptDeliveryRecordBack updObj = new BusReceiptDeliveryRecordBack();
            BeanUtil.copyProperties(inObj, updObj);
            updObj.setRecParentId(inObj.getRecDecId());
            updObj.setSyncStatus(ApiConstants.REC_BK_DELIVERY_INT);
            upds.add(updObj);
        });
        if (0 < upds.size()) {
            bkReckServices.saveBatch(upds);
        }
    }
}
